package com.hsnn.medstgmini.pro.base.dao.Impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.dict.dao.impl.BaseHibernateDAO;
import com.hsnn.medstgmini.dao.HibernateSqlUtil;
import com.hsnn.medstgmini.pro.base.dao.BaseShortageDao;
import com.hsnn.medstgmini.pro.base.model.BaseShortage;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 缺货申报
 * @author 应晓川
 * @date 2015年11月27日10:07:01
 */
@Repository
public class BaseShortageDaoImpl extends BaseHibernateDAO implements BaseShortageDao {

	@Autowired
	private HibernateSqlUtil dao;

	/**
	 * @category 获取详情
	 * @date 2015年11月27日10:16:47
	 * @param page
	 * @return
	 */
	@Override
	public Pagination getShortageInfo(Pagination page) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * @category 获取列表
	 * @date 2015年11月27日10:16:47
	 * @param page
	 * @return
	 */
	@Override
	public Pagination getShortageList(Pagination page) {
		StringBuffer sb = new StringBuffer();
		
		return null;
	}
	/**
	 * @category 医院根据条件查询品规
	 * @param @return
	 */
	@Override
	public Pagination getDrugqualityspecListByHosp(Pagination page) {
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT A.qualityspec_id as qualitySpecId, A.qualityspec_name AS qualitySpecName, A.qualityspec_code AS qualitySpecCode, ");
		sql.append(" A.qualityspec_name_spel AS qualitySpecNameSpel,A.form_name AS formName,A.spec_name AS specName,A.status AS status, ");
		sql.append(" A.form AS form,A.form_status AS formStatus,A.spec AS spec,A.form_code AS formCode,A.spec_code AS specCode,B.submit_datetime submitDatetime");
		sql.append(" FROM std_drugqualityspec A left join base_shortage B on B.qualityspec_code=A.qualityspec_code and B.hosp_code=:hospCode where A.status=1 ");
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("hospCode",page.getConditions().get("hospCode"));
		if(StringUtils.isNotBlank(page.getConditions().get("drugQualitySpecCode"))){
			sql.append(" and A.qualityspec_code = :drugQualitySpecCode");
			params.put("drugQualitySpecCode", page.getConditions().get("drugQualitySpecCode"));
		}
		if(StringUtils.isNotBlank(page.getConditions().get("drugQualitySpecName"))){
			sql.append(" and ( A.qualityspec_name like :drugQualitySpecName OR A.qualityspec_name_spel like :drugQualitySpecName )");
			params.put("drugQualitySpecName", "%" +page.getConditions().get("drugQualitySpecName")+"%");
		}
		if(StringUtils.isNotBlank(page.getConditions().get("formName"))){
			sql.append(" and A.form_name like :formName");
			params.put("formName", "%"+page.getConditions().get("formName")+"%");
		}
		if(StringUtils.isNotBlank(page.getConditions().get("specName"))){
			sql.append(" and A.spec_name like :specName");
			params.put("specName", "%"+page.getConditions().get("specName")+"%");
		}
		sql.append(" order by A.qualityspec_code desc");
		return dao.getSqlPageMapList(sql.toString(), params, page);
	}

 
	@Override
	public Pagination getFeedbackListByHosp(Pagination page) {
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT  A.qualityspec_name AS qualitySpecName, A.qualityspec_code AS qualitySpecCode,ds.dic_name feedbackForm,  ");
		sql.append(" A.form_name AS formName,A.spec_name AS specName,sh.hosp_classify hospClassify,B.* ");
		sql.append(" FROM base_shortage B  left join std_drugqualityspec A on B.qualityspec_code=A.qualityspec_code and A.status=1 ")
		.append(" left join dic_sysdic ds on ds.dic_code = B.feedback and ds.classify_code=124 ")
		.append(" left join std_hospinfo sh on sh.hosp_code= B.hosp_code ")
		.append(" where B.hosp_code=:hospCode ");
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("hospCode",page.getConditions().get("hospCode"));
		if(StringUtils.isNotBlank(page.getConditions().get("drugQualitySpecCode"))){
			sql.append(" and A.qualityspec_code = :drugQualitySpecCode");
			params.put("drugQualitySpecCode", page.getConditions().get("drugQualitySpecCode"));
		}
		if(StringUtils.isNotBlank(page.getConditions().get("drugQualitySpecName"))){
			sql.append(" and ( A.qualityspec_name like :drugQualitySpecName OR A.qualityspec_name_spel like :drugQualitySpecName )");
			params.put("drugQualitySpecName", "%" +page.getConditions().get("drugQualitySpecName")+"%");
		}
		if(StringUtils.isNotBlank(page.getConditions().get("formName"))){
			sql.append(" and A.form_name like :formName");
			params.put("formName", "%"+page.getConditions().get("formName")+"%");
		}
		if(StringUtils.isNotBlank(page.getConditions().get("specName"))){
			sql.append(" and A.spec_name like :specName");
			params.put("specName", "%"+page.getConditions().get("specName")+"%");
		}
		sql.append(" order by A.qualityspec_code desc");
		return dao.getSqlPageMapList(sql.toString(), params, page);
	}


	@Override
	public Object getBaseShortageById(String shortageId) {
			StringBuffer sql = new StringBuffer();
			sql.append(" SELECT  A.qualityspec_name qualityspecName, A.qualityspec_code qualityspecCode,ds.dic_name feedbackForm, ");
			sql.append(" A.form_name AS formName,A.spec_name AS specName,B.* ");
			sql.append(" FROM base_shortage B  left join std_drugqualityspec A on B.qualityspec_code=A.qualityspec_code and A.status=1   ")
			.append(" left join dic_sysdic ds on ds.dic_code = B.feedback and ds.classify_code=124 ")
			.append(" where B.shortage_id=:shortageId ");
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("shortageId", shortageId);
			return dao.getSqlMapUniqueResult(sql.toString(), params);
	}

	/**
	 * 按条件获取审核列表
	 * @param page
	 * @return
	 */
	@Override
	public Pagination getAuditList(Pagination page) {
		String zxAuditTag=page.getConditions().get("zxAuditTag");
		String heaBurId=page.getConditions().get("heaBurId");
		
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT B.shortage_id , A.qualityspec_name AS qualitySpecName, A.qualityspec_code AS qualitySpecCode, ");
		sql.append(" A.form_name AS formName,A.spec_name AS specName,B.bur_audit_status,B.cet_audit_status,B.hosp_code hospCode,B.hosp_name hospName ");
		sql.append(" FROM base_shortage B  left join std_drugqualityspec A on B.qualityspec_code=A.qualityspec_code and A.status=1 ");
		Map<String,Object> params = new HashMap<String,Object>();
		if(StringUtils.isNotBlank(zxAuditTag)){
			int temp = Integer.parseInt(zxAuditTag);
			if(Constants.COUNTY_HOSP==temp){
				sql.append(" where B.bur_audit_status is null ");
			}else if(Constants.BASE_HOSP==temp){
				sql.append(" where B.bur_audit_status = 1  ");
			}
		}else{
			if(StringUtils.isNotBlank(heaBurId)){
				sql.append(" where B.hosp_code in (")
				   .append(" select sh.hosp_code from   std_hospinfo sh  join std_healthbureau shb on shb.area_id=sh.area_id and shb.hea_bur_id =:heaBurId and shb.status=1 ")
				   .append(" where sh.status=1 and sh.hosp_classify=2 ) ");
				params.put("heaBurId", heaBurId);
			}
		}
		if(StringUtils.isNotBlank(page.getConditions().get("drugQualitySpecCode"))){
			sql.append(" and A.qualityspec_code = :drugQualitySpecCode");
			params.put("drugQualitySpecCode", page.getConditions().get("drugQualitySpecCode"));
		}
		if(StringUtils.isNotBlank(page.getConditions().get("drugQualitySpecName"))){
			sql.append(" and ( A.qualityspec_name like :drugQualitySpecName OR A.qualityspec_name_spel like :drugQualitySpecName )");
			params.put("drugQualitySpecName", "%" +page.getConditions().get("drugQualitySpecName")+"%");
		}
		if(StringUtils.isNotBlank(page.getConditions().get("formName"))){
			sql.append(" and A.form_name like :formName");
			params.put("formName", "%"+page.getConditions().get("formName")+"%");
		}
		if(StringUtils.isNotBlank(page.getConditions().get("specName"))){
			sql.append(" and A.spec_name like :specName");
			params.put("specName", "%"+page.getConditions().get("specName")+"%");
		}
		sql.append(" order by A.qualityspec_code desc");
		return dao.getSqlPageMapList(sql.toString(), params, page);
	}

 
	@Override
	public void updateByWSJ(BaseShortage baseShortage) {
		Session session = getSession();
		StringBuffer sql = new StringBuffer()
		.append("  update base_shortage set bur_audit_status=:burAuditStatus ,bur_audit_remark=:burAuditRemark, bur_audit_datetime=:now,is_readed=1,")
		.append(" upd_datetime=:now,upd_user=:updUser ");
		if(baseShortage.getBurAuditStatus()==1){
			sql.append(" ,cet_audit_status=0 ");
		}
		sql.append(" where shortage_id=:shortageId ");
		Date now = new Date();
		SQLQuery query = session.createSQLQuery(sql.toString());
		query.setParameter("burAuditStatus", baseShortage.getBurAuditStatus())
		.setParameter("burAuditRemark", baseShortage.getBurAuditRemark())
		.setParameter("now", now)
		.setParameter("updUser", baseShortage.getUpdUser())
		.setParameter("shortageId", baseShortage.getShortageId())
		.executeUpdate();
		session.flush();
		session.clear();
	}

	 
	@Override
	public void updateByZX(BaseShortage baseShortage) {
		Session session = getSession();
		StringBuffer sql = new StringBuffer()
		.append("  update base_shortage set cet_audit_status=:cetAuditStatus ,cet_audit_remark=:cetAuditRemark, cet_audit_datetime=:now,is_readed=1,")
		.append(" feedback=:feedback, ")
		.append(" upd_datetime=:now,upd_user=:updUser ");
		sql.append(" where shortage_id=:shortageId ");
		Date now = new Date();
		SQLQuery query = session.createSQLQuery(sql.toString());
		query.setParameter("cetAuditStatus", baseShortage.getCetAuditStatus())
		.setParameter("cetAuditRemark", baseShortage.getCetAuditRemark())
		.setParameter("now", now)
		.setParameter("feedback", baseShortage.getFeedback())
		.setParameter("updUser", baseShortage.getUpdUser())
		.setParameter("shortageId", baseShortage.getShortageId())
		.executeUpdate();
		session.flush();
		session.clear();
	}

	
}
